package app.util.image.action
{
	
	import flash.display.*;
	import flash.filters.*;
	import flash.geom.*;
	
	public class RedRepairAction extends PartialProcessingAction
	{	
		public override function getDescription():String
		{
			return "Redeye Repair";
		}
		
		protected override function doParticalProcess(cur:BitmapData, p:Array, str:Number):BitmapData
		{			
			var ret:BitmapData = cur.clone();
		 	var startX:int = p[0] - p[2] / Math.sqrt(2);
		 	var startY:int = p[1] - p[2] / Math.sqrt(2);
		 	var endX:int = p[0] + p[2] / Math.sqrt(2);
		 	var endY:int = p[1] + p[2] / Math.sqrt(2);
		 	
		 	var isRedPixel:Function = function(i:int, j:int):Boolean
		 	{
		 		var color:uint = ret.getPixel(i, j);
		 		var r:uint = (color & 0x00ff0000) >> 16;
				var g:uint = (color & 0x0000ff00) >> 8;
				var b:uint = color & 0x000000ff;
				
				return (r + 10) > (g + b);
		 	}
		 	
		 	var removeRedPixel:Function = function(i:int, j:int):void
		 	{
		 		var color:uint = ret.getPixel(i, j);
		 		var a:uint = (color & 0xff000000) >> 24;
				var g:uint = (color & 0x0000ff00) >> 8;
				var b:uint = color & 0x000000ff;		 		
				var n:uint = (g + b) / 2;
				
				var nColor:uint = (b | g << 8 | n << 16 | a << 24);
				ret.setPixel(i, j, nColor);
		 	}
		 	
		 	for(var i:int = startX; i <= endX; i++)
		 	{
		 		for(var j:int = startY; j <= endY; j++)
		 		{
		 		 	if(isRedPixel(i, j))
		 		 	{
		 		 		removeRedPixel(i, j);
		 		 	}
		 		}
		 	}
		 	
            return ret;
		}
	}
}
